<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/royal') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
  },
  data() {
    return {
      chart: null,
      opinion:{
        backgroundColor: '#fff',
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {},
        dataset: {
          dimensions: ['product', '正面', '中性', '负面'],
          source: [
            {product: '微信', '正面': 43.3, '中性': 85.8, '负面': 93.7},
            {product: '新闻', '正面': 83.1, '中性': 73.4, '负面': 55.1},
            {product: '论坛', '正面': 86.4, '中性': 65.2, '负面': 82.5},
            {product: '贴吧', '正面': 72.4, '中性': 53.9, '负面': 39.1}
          ]
        },
        xAxis: {type: 'category'},
        yAxis: {},
        series: [
          {type: 'bar',barMaxWidth:50,},
          {type: 'bar',barMaxWidth:50,},
          {type: 'bar',barMaxWidth:50,}
        ]
      }
    }
  },
  watch:{

  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'royal')
      this.chart.setOption(this.opinion)
    }
  }
}
</script>
